﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Web;
using System.IO;
using System.Configuration;

namespace Petaframe.Configuration
{
    public abstract class ConfigReaderBase
    {
        protected readonly XDocument xdoc;
        protected IDictionary<string,IEnumerable<XElement>> configElements;

        public ConfigReaderBase()
        {
            if (HttpContext.Current != null)
            {
                xdoc = XDocument.Load(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "web.config"));
            }
            else if(File.Exists(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath))
            {             
                  // AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                xdoc = XDocument.Load(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath);
               
            }
            else
            {
                throw new FileNotFoundException("Uygulamada konfigürasyon dosyası bulunamadı.Uygulamanıza app.config veya web.config dosyası ekleyiniz.");
            }

            configElements = new Dictionary<string, IEnumerable<XElement>>();
           
               
        }

        public ConfigReaderBase(string pysicalConfigFilePath)
        {
            if (string.IsNullOrWhiteSpace(pysicalConfigFilePath))
                throw new ArgumentNullException("pysicalConfigFilePath");
            
            xdoc = XDocument.Load(pysicalConfigFilePath);
        }

        public abstract IDictionary<string, IEnumerable<XElement>> ReadConfig();
       
    }
}
